package com.hdoit.apollo.web.shop.controllers;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.common.enums.shop.CustomerRecordType;
import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.CustomerRecord;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.service.ICustomerRecordService;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.viewmodel.shop.CustomerRecordSearch;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年06月08日 15:12:15
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/customerRecord/")
public class CustomerRecordController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/customerRecord/";
    }

    @Autowired
    private ICustomerRecordService customerRecordService;
    
    @Autowired
    private ICustomerService customerService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="customerRecordDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CustomerRecordSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(customerRecordService.queryCountMP(search));
        List<CustomerRecord> customerRecords= customerRecordService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("customerRecords", customerRecords);
        model.addAttribute("customerRecordSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String customerRecordDetailed(Model model,String key,String customerId){
        CustomerRecord customerRecord=new CustomerRecord();
        if(!StringUtils.isEmpty(key)){
            customerRecord=customerRecordService.queryModel(key);
        }
        if(!StringUtils.isEmpty(customerId)){
            customerRecord.setCustomerId(customerId);
            Customer customer=customerService.queryModel(customerId);
            customerRecord.setReceivable(customer.getCustomerDebt());
        }
        model.addAttribute("customerRecord", customerRecord);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,CustomerRecord customerRecord){
        if(StringUtils.isEmpty(customerRecord.getRecordId())){
            customerRecord.setRecordId(CommonUtils.getKeyId());
            customerRecord.setCreateDate(DateUtils.getCurrent());
            customerRecord.setDeleteMark(YesOrNoInteger.no);
            customerRecord.setCreateUserId(getLoginModel().getLoginId());
            customerRecord.setCreateUserName(getLoginModel().getLoginName());
            customerRecord.setType(CustomerRecordType.adjustment);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            customerRecord.setShopId(shop.getShopId());
            customerRecord.setModelStatus(ModelStatus.add);
        }else{
            customerRecord.setModifyDate(DateUtils.getCurrent());
            customerRecord.setModifyUserId(getLoginModel().getLoginId());
            customerRecord.setModifyUserName(getLoginModel().getLoginName());
            customerRecord.setModelStatus(ModelStatus.update);
        }
        
    	Customer customer=customerService.queryModel(customerRecord.getCustomerId());
    	customer.setCustomerDebt(customerRecord.getReceipt());
    	customer.setModelStatus(ModelStatus.update);
    	customerService.applyChange(customer);

        ActionResult actionResult;
        if(customerRecordService.applyChange(customerRecord)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("customerRecord", customerRecord);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(customerRecordService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}